
import langdetect
import re
import string

# 去除特殊字符
remove_special_chars = u'[’·°–!"#$%&\'()*+,-./:;<=>?@，。?★、…【】（）《》？“”‘’！[\\]^_`{|}~]+'
punctuation_map = dict((ord(char), None) for char in string.punctuation)
re_words_en = re.compile(u"[a-zA-Z]")
re_words_zh = re.compile(u"[\u4e00-\u9fa5]+")
re_words_ko = re.compile(u"[\uac00-\ud7ff]+")
re_words_ja = re.compile(u"[\u30a0-\u30ff\u3040-\u309f]+")

def filter_sentence(sentence):
    sentence = re.sub(remove_special_chars, '', sentence)
    sentence = sentence.translate(punctuation_map)
    return sentence.strip()

def detect_language(s):
    s = filter_sentence(s)
    result = []

    s = re.sub('[0-9]', '', s).strip()

    # 检测英文
    res = re.findall(re_words_en, s)
    res2 = re.sub('[a-zA-Z]', '', s).strip()
    if len(res) > 0:
        result.append('en')
    if len(res2) <= 0:
        return 'en'

    # 检测中文
    res = re.findall(re_words_zh, s)
    res2 = re.sub(u"[\u4e00-\u9fa5]+", '', s).strip()
    if len(res) > 0:
        result.append('zh')
    if len(res2) <= 0:
        return 'zh'

    # 检测韩文
    res = re.findall(re_words_ko, s)
    res2 = re.sub(u"[\uac00-\ud7ff]+", '', s).strip()
    if len(res) > 0:
        result.append('ko')
    if len(res2) <= 0:
        return 'ko'

    # 检测日文
    res = re.findall(re_words_ja, s)
    res2 = re.sub(u"[\u30a0-\u30ff\u3040-\u309f]+", '', s).strip()
    if len(res) > 0:
        result.append('ja')
    if len(res2) <= 0:
        return 'ja'

    if result == None:
        return False
    result = langdetect.detect(s)
    return ','.join(result)

def language_detect(text):
    return detect_language(text)

if __name__ == '__main__':
    text = 'manipulating'
    print(language_detect(text))


